package cn.wolfcode.util;

import cn.wolfcode.rbac.domain.Employee;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Component
public class UserContext {

    private static HttpSession getHttpSession(){
        ServletRequestAttributes attrs = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //往session 存入员工
    public static void setEmployee(Employee employee){
        HttpSession session = getHttpSession();
        session.setAttribute("EMPLOYEE_IN_SESSION",employee);
    }

    // 往session 取员工
    public static Employee getEmployee(){
        return (Employee) getHttpSession().getAttribute("EMPLOYEE_IN_SESSION");
    }

    // 往session 存入权限表达式
    public static void setExpressions(List<String> expressions){
        getHttpSession().setAttribute("EMPLOYEE_IN_SESSION",expressions);
    }

    // 从session取权限表达式
    public static List<String> getExpression(){
        return (List<String>)getHttpSession().getAttribute("EMPLOYEE_IN_SESSION");
    }

    // 从session 移除权限表达式
    public static void removeExpression(){
        getHttpSession().removeAttribute("EMPLOYEE_IN_SESSION");
    }


}
